/**!
 *
 * @file           : /rt_ws2812b/example/ws2812b_example.c
 *
 * @date           : 2020-07-16 21:24:54
 *
 * @author         : maplerian
 *
 * @brief          : file content
 *
 */

#include <module_usb.h>
#include <rtthread.h>
#include <drv_ws2812.h>
#include <pin_dev_cfg.h>
#include <thread_cfg.h>
// #include <Blues_keymap.h>

#define MATRIX_ROWS 5
#define MATRIX_COLS 6

extern const rt_uint8_t** led_index;

#ifndef WS2812B_EXAMPLE_SPI_NAME
    #define WS2812B_EXAMPLE_SPI_NAME    "spi30"
#endif  //!WS2812B_EXAMPLE_SPI_NAME

#ifndef WS2812B_EXAMPLE_NODE_LENGTH
    #define WS2812B_EXAMPLE_NODE_LENGTH 27
#endif // !WS2812B_EXAMPLE_NODE_LENGTH

#define pin_control rt_pin_get("PD.0")
const uint32_t led_play_data[] = {0x101000, 0x100000, 0x081000, 0x000010, 0x001000, 0x101010, 0x100300, 0x100020, 0x000812, 0x330010};
static ws2812_dev_t ws2812;

extern rt_event_t p_usb_event;
extern rt_mq_t mqt_sync_keyboard_led;

static rt_thread_t thread_led;

void thread_led_init(void);
static void thread_led_entry(void* param);

static void thread_led_entry(void* param)
{
    rt_uint8_t index_temp;

    while(1)
    {
        // if(PIN_HIGH ==  rt_pin_read(pin_control))
        // {
            for(rt_uint8_t i = 0; i < 10; i++)
            {
                ws2812_write_rgb_to_all_24bit(ws2812, led_play_data[i]);
                ws2812_send(ws2812);
                rt_thread_mdelay(200);
            }
        // }
        // else
        // {
        //     // if(rt_mq_recv(mqt_sync_keyboard_led, &index_temp, sizeof(index_temp), 10) == RT_EOK)
        //     // {
        //     //     // ws2812_write_rgb_to_all_24bit(ws2812, 0x000000);
        //     //     ws2812_write_rgb_to_node(ws2812, (uint16_t)(*(*(led_index+(index_temp/MATRIX_ROWS)) + (index_temp%MATRIX_COLS))), 60, 0, 60);
        //     //     ws2812_send(ws2812);
        //     //     rt_thread_mdelay(20);
        //     //     ws2812_write_rgb_to_all_24bit(ws2812, 0x000000);
        //     //     ws2812_send(ws2812);
        //     // }
        // }

        rt_thread_mdelay(200);
    }
}

void thread_led_init(void)
{
    rt_pin_mode(pin_control, PIN_MODE_INPUT); //Hardware pulldown
    rt_hw_spi_device_attach(LED_DATA_SPI_BUS_NAME, LED_DATA_SPI_NAME, GPIOB, GPIO_PIN_7);
    rt_device_t ws2812b_spi = rt_device_find(WS2812B_EXAMPLE_SPI_NAME);

    if(ws2812b_spi == RT_NULL)
    {
        rt_kprintf("Not Find Spi Device: %s.\r\n", WS2812B_EXAMPLE_SPI_NAME);
        return;
    }

    ws2812 = ws2812_create(WS2812B_EXAMPLE_SPI_NAME, WS2812B_EXAMPLE_NODE_LENGTH);

    if(!ws2812)
    {
        rt_kprintf("create ws2812 object faild.\r\n");
        return;
    }

    thread_led = rt_thread_create("tled",
                                  thread_led_entry,
                                  RT_NULL,
                                  THREAD_STACK_SIZE_SMALL,
                                  THREAD_PRIORITY_LV3,
                                  THREAD_TIMESLICE_SHORT);

    if(thread_led != RT_NULL)
    {
        rt_thread_startup(thread_led);
    }
}
INIT_APP_EXPORT(thread_led_init);



int ws2812b_test(int argc, char* argv[])
{
    //  低于这个值可能会造成某节点无法显示这个值
#define COLOR_MIN_VALUE   50
    ws2812_write_rgb_to_all(ws2812, COLOR_MIN_VALUE, 0, 0);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    ws2812_write_rgb_to_all(ws2812, 0, COLOR_MIN_VALUE, 0);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    ws2812_write_rgb_to_all(ws2812, 0, 0, COLOR_MIN_VALUE);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    ws2812_write_rgb_to_all(ws2812, COLOR_MIN_VALUE, COLOR_MIN_VALUE, 0);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    ws2812_write_rgb_to_all(ws2812, COLOR_MIN_VALUE, 0, COLOR_MIN_VALUE);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    ws2812_write_rgb_to_all(ws2812, 0, COLOR_MIN_VALUE, COLOR_MIN_VALUE);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    ws2812_write_rgb_to_all(ws2812, COLOR_MIN_VALUE, COLOR_MIN_VALUE, COLOR_MIN_VALUE);
    ws2812_send(ws2812);
    rt_thread_mdelay(500);
    rt_free((void*)ws2812);
    return 1;
}
MSH_CMD_EXPORT(ws2812b_test, test ws2812b function);

